package com.wpq.game_company.controller;


import com.wpq.game_company.domain.Game;
import com.wpq.game_company.service.IGameService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;


import java.util.List;
@Controller
@RequestMapping("/game")
public class GameController {

    @Autowired
    private IGameService gameService;

    //展示
    @RequestMapping( "/allgame")
    public String gamelist(Model model){
        List<Game> games=gameService.selectAll();
        model.addAttribute("games",games);
        return "gameindex";
    }

    //增加
    @RequestMapping("/toaddgame")
    public String toaddgame(){
        return "addgame";
    }

//    @RequestMapping("/addgame")
//    public String addgame(Game game){
//        gameService.insert(game);
//        return "redirect:/game/allgame";
//    }
    @RequestMapping("/addgame")
    public String addgame(Game game){
        gameService.insert(game);
        return "suc_g";
    }



    //删除
    @RequestMapping( "/removegame{id}")
    public String removegame(@PathVariable("id") int id){

        gameService.delete(id);

        return "redirect:/game/allgame";
    }

    //修改
    @RequestMapping("/toupdatagame{id}")
    public String toupdatagame(@PathVariable("id") int id,Model model){
        Game findgame=gameService.findgameByid(id);
        model.addAttribute("findgame",findgame);

        return "updatagame";
    }

    @RequestMapping("/updatagame")
    public String updatagame(Game game){
        gameService.updata(game);
        return "redirect:/game/allgame";
    }

    @RequestMapping("/searchgame")
    public String searchgame(String type,Model model){
        List<Game> searchgames= gameService.searchAccording(type);
        model.addAttribute("searchgames",searchgames);
        if(!type.equals("")){
            return "searchgame";
        }
        return "redirect:/game/allgame";

    }
}
